package app.fugema.shop.main.login.view;

import com.base.common.model.http.err.ApiException;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseActivity;
import com.base.common.viewmodel.BaseViewObserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityBindAccountBinding;
import app.fugema.shop.main.login.model.MUserInfo;
import app.fugema.shop.main.login.model.OauthInfoBean;
import app.fugema.shop.main.login.model.RegisterResult;
import app.fugema.shop.main.login.vm.AppBindVM;

public class BindAccountActivity extends BaseActivity<ActivityBindAccountBinding, AppBindVM>
{
    //第三方 登录 登录后 绑定账号

    public static void lunch(Context context , OauthInfoBean oauthInfo)
    {
        Intent intent = new Intent(context, BindAccountActivity.class);
        intent.putExtra( "auth", JacksonUtils.transBean2Json( oauthInfo ));
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_account);
    }

    private OauthInfoBean oauthInfoBean;

    @Override
    protected void initView()
    {

        binding.tvTitle.setText("绑定账户");
        binding.ivBack.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                finish();
            }
        });
        // 绑定账户
        binding.btnBind.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                String phone = binding.etPhoneNO.getText().toString().trim();
                String password = binding.etPwdCode.getText().toString();
                if(UIUtils.isNotEmpty(phone) && UIUtils.isNotEmpty(password) )
                {
                    if( UIUtils.isNotEmpty( oauthInfoBean ) )
                    {
                        viewModel.appLoginByPwd(phone,password,oauthInfoBean)
                                 .observe(BindAccountActivity.this, new BaseViewObserver<LiveDataWrapper<MUserInfo>>()
                                 {
                                     @Override
                                     protected void onSuccess(LiveDataWrapper<MUserInfo> data)
                                     {
                                         if( data.data.getCode() == 103 )
                                         {
                                             UIUtils.showToastSafesClose(data.data.getMsg());
                                         }
                                         else
                                         {
                                             UIUtils.showToastSafes("登录成功");
                                             finish();
                                         }
                                     }

                                     @Override
                                     protected void onAppError(ApiException error)
                                     {
                                         super.onAppError(error);
                                         UIUtils.showToastSafes(error.getMessage());
                                     }
                                 });
                    }
                }
                else
                {
                   UIUtils.showToastSafes("不可为空");
                }
            }
        });
        //直接注册
        binding.tvRegistTips.setOnClickListener(new OnClickCheckedUtil()
        {
            @Override
            public void onClicked(View view)
            {
                if( UIUtils.isNotEmpty( oauthInfoBean ) )
                {
                    viewModel.appRegister( oauthInfoBean )
                             .observe(BindAccountActivity.this, new BaseViewObserver<LiveDataWrapper<RegisterResult>>()
                             {
                                 @Override
                                 protected void onSuccess(LiveDataWrapper<RegisterResult> data)
                                 {
                                     LogUtil.e("appRegister" + JacksonUtils.transBean2Json( data.data )  );
                                 }
                             });
                }
            }
        });
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);

        String tempStr = intent.getStringExtra("auth");
        if(UIUtils.isNotEmpty( tempStr ))
        {
            oauthInfoBean = JacksonUtils.getJsonBean( tempStr,OauthInfoBean.class );
        }
    }
}
